
package org.varan.emulator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Main {
		
	private interface ControlButton {
		public Button createButton(Composite parent);
	}
	
	private static ControlButton playPause = new ControlButton() {
		private final static String PLAY = "Play";
		private final static String PAUSE = "Pause";
		private final Image PLAY_IMAGE = new Image(null, "images/play.png");
		private final Image PAUSE_IMAGE = new Image(null, "images/pause.png");
		private final Message PLAY_MESSAGE = new Message("Play");
		private final Message PAUSE_MESSAGE = new Message("Pause");
				
		@Override
		public Button createButton(Composite parent) {
			final Button button = new Button(parent, SWT.TOGGLE);
			button.setImage(PLAY_IMAGE);
			button.setToolTipText(PLAY);
			button.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent e) {
					if (button.getSelection()) {
						button.setToolTipText(PAUSE);
						button.setImage(PAUSE_IMAGE);
						RemoteControlClient.INSTANCE.send(PLAY_MESSAGE);
					} else {
						button.setToolTipText(PLAY);
						button.setImage(PLAY_IMAGE);
						RemoteControlClient.INSTANCE.send(PAUSE_MESSAGE);
					}
					
					button.getParent().layout();
					
				}
			});
			
			return button;
		}
		
	};
	
	private enum PushButtons implements ControlButton {
		PREV("Prev", new Image(null, "images/prev.png")), 
		NEXT("Next", new Image(null, "images/next.png")), 
		STOP("Stop", new Image(null, "images/stop.png"));
		
		public final String title;
		private final Image image;
		private final Message message;

		private PushButtons(String title, Image image) {
			this.title = title;
			this.message = new Message(title);
			this.image = image;
		}
		
		@Override
		public Button createButton(Composite parent) {
			Button button = new Button(parent, SWT.PUSH);
			button.setToolTipText(title);
			button.setImage(image);
			button.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent e) {
					RemoteControlClient.INSTANCE.send(message);
				}
			});
			
			return button;
		}		
	}
	
	public static void main(String[] args)  {
		Display display = Display.getCurrent();
		Shell shell = new Shell(display);
		createUI(shell);
		shell.setText("");
		shell.open();
		shell.setSize(300, 200);
		
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose();
		RemoteControlClient.INSTANCE.close();
	}
	
	private static void createUI(Composite parent) {
		parent.setLayout(new GridLayout(4, false));
		
		PushButtons.PREV.createButton(parent);
		playPause.createButton(parent);
		PushButtons.STOP.createButton(parent);
		PushButtons.NEXT.createButton(parent);		
	}
}
